package com.example.android_demo2.workmanager

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.work.*
import java.util.concurrent.TimeUnit

class WorkManagerViewModel(application: Application) : AndroidViewModel(application) {
    @SuppressLint("StaticFieldLeak")
    private val context = getApplication<Application>().applicationContext

    private val workManager = WorkManager.getInstance(context)
    val workInfo = workManager.getWorkInfosByTagLiveData("log_tag")
    private val constraints = Constraints.Builder()
        .setRequiredNetworkType(NetworkType.UNMETERED)
        .setRequiresCharging(false)
        .build()

    private val printWorkRequest: WorkRequest = OneTimeWorkRequestBuilder<LogWorker>()
        .setConstraints(constraints)
        .setInitialDelay(10, TimeUnit.SECONDS)
        .setInputData(workDataOf("IMAGE_DATA" to ""))
        .addTag("log_tag")
        .build()
    fun applyWorker() {
        workManager.enqueue(printWorkRequest)
    }

}